<template>
  <div id="userInformationPage" class="user-info-container">
    <div class="avatar-section">
      <AvatarUpload :url="url" :on-success="onSuccess" />
    </div>

    <div class="user-info-card">
      <div class="user-info-row">
        <div class="label">用户名：</div>
        <div v-if="!isEditing" class="value-wrapper">
          <span class="value">{{ loginUser.userName || '未设置' }}</span>
          <a-button
            type="link"
            size="small"
            @click="startEditing"
            class="edit-btn"
          >
            <EditOutlined /> 修改
          </a-button>
        </div>

        <div v-else class="edit-form-wrapper">
          <a-input
            v-model:value="tempName"
            placeholder="请输入用户名"
            @pressEnter="save"
            class="input-field"
          />

          <div class="button-group">
            <a-button
              type="primary"
              size="small"
              @click="save"
              class="save-btn"
            >
              <CheckOutlined /> 保存
            </a-button>

            <a-button
              type="link"
              size="small"
              @click="cancel"
              class="cancel-btn"
            >取消</a-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import AvatarUpload from '@/components/AvatarUpload.vue'
import { useLoginUserStore } from '@/stores/useLoginUserStore'
import { onMounted, reactive, ref } from 'vue'
import { EditOutlined, CheckOutlined } from '@ant-design/icons-vue'
import { message } from 'ant-design-vue'
import { updateUserUsingPost } from '@/api/yonghuxiangguanjiekou'
import router from '@/router'

const url = ref<string>()
const onSuccess = async (newUrl: string) => {
  url.value = newUrl
  await loginUserStore.fetchLoginUser()
}

const loginUserStore = useLoginUserStore()
const loginUser = loginUserStore.loginUser

const getOldUser = () => {
  // 未登录无法上传头像
  if (!loginUser.id) {
    return false
  }

  //检查是否有头像
  if (loginUser.userAvatar !== null && loginUser.userAvatar !== '') {
    url.value = loginUser.userAvatar
  } else {
    url.value = '<null>'
  }
}
onMounted(() => {
  getOldUser()
})

const isEditing = ref(false)
const tempName = ref<string>()

const startEditing = () => {
  tempName.value = loginUser.userName
  isEditing.value = true
}
const save = async () => {
  if(tempName.value === '') {
    message.error("用户名不能为空")
  }

  const res = await updateUserUsingPost({
    id: loginUser.id,
    userName: tempName.value || '无名',
  })
  if (res.data.code === 0) {
    message.success('修改成功')
    // 刷新数据
    window.location.reload()
  } else {
    message.error('修改失败')
  }
  isEditing.value = false
}
const cancel = () => {
  isEditing.value = false
}
</script>
<style scoped>
.user-info-container {
  max-width: 800px;
  margin: 30px auto;
  padding: 24px;
  background-color: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.06);
  transition: all 0.3s ease;
}

.avatar-section {
  text-align: center;
  margin-bottom: 32px;
}

.user-info-card {
  padding: 24px;
  background-color: #f8f9fa;
  border-radius: 8px;
}

.user-info-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 0;
  border-bottom: 1px solid #e9ecef;
}

.label {
  font-weight: 500;
  color: #333;
  min-width: 80px;
}

.value-wrapper {
  display: flex;
  align-items: center;
  gap: 8px;
}

.value {
  font-size: 16px;
  color: #6c757d;
}

.edit-btn {
  color: #1890ff;
  transition: color 0.2s;
}

.edit-btn:hover {
  color: #096dd9;
}

.edit-form-wrapper {
  display: flex;
  align-items: center;
  gap: 16px;
  width: 100%;
}

.input-field {
  flex-grow: 1;
  max-width: 400px;
}

.button-group {
  display: flex;
  gap: 8px;
}

.save-btn {
  background-color: #1890ff !important;
  border-color: #1890ff !important;
}

.save-btn:hover {
  background-color: #096dd9 !important;
  border-color: #096dd9 !important;
}

.cancel-btn {
  color: #6c757d;
  transition: color 0.2s;
}

.cancel-btn:hover {
  color: #333;
}

/* 响应式设计 */
@media (max-width: 640px) {
  .user-info-container {
    margin: 16px;
    padding: 16px;
  }

  .user-info-row {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }

  .label {
    min-width: auto;
    margin-bottom: 8px;
  }
}

/* 动画效果 */
.user-info-card {
  transform: translateY(0);
  opacity: 1;
  transition: all 0.3s ease;
}

.user-info-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.08);
}
</style>
